﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using LorraineSec.DatabaseLib;

namespace LorraineSec.DatabaseLib.DataObjects
{
    public class Event
    {
        #region Constants
        private const string ROOT_STR = XmlDatabase.EVENT;
        private const string ID_STR = XmlDatabase.ID;
        private const string TIME_STR = XmlDatabase.TIME;
        private const string COMP_STR = XmlDatabase.COMPID;
        private const string USER_STR = XmlDatabase.USERID;
        private const string ASSET_STR = XmlDatabase.ASSETID;
        private const string DEVICE_STR = XmlDatabase.DEVICEID;
        private const string CONTENT_STR = XmlDatabase.CONTENT;
        #endregion

        private XmlNode xmlEventRoot;
        private XmlNode xmlTime;
        private XmlNode xmlCompartmentID;
        private XmlNode xmlUserID;
        private XmlNode xmlAssetID;
        private XmlNode xmlDeviceSerial;
        private XmlNode xmlContent;

        public Event(XmlNode node)
        {
            xmlEventRoot = node;
            xmlTime = xmlEventRoot.SelectSingleNode(TIME_STR);
            xmlCompartmentID = xmlEventRoot.SelectSingleNode(COMP_STR);
            xmlUserID = xmlEventRoot.SelectSingleNode(USER_STR);
            xmlAssetID = xmlEventRoot.SelectSingleNode(ASSET_STR);
            xmlDeviceSerial = xmlEventRoot.SelectSingleNode(DEVICE_STR);
            xmlContent = xmlEventRoot.SelectSingleNode(CONTENT_STR);
        }

        public static List<Event> GetList(XmlDatabase db)
        {
            List<Event> retList = new List<Event>();
            if (db.logs != null)
            {
                XmlNodeList nodeList = db.logs.SelectNodes(ROOT_STR);
                retList.Capacity = nodeList.Count;
                foreach (XmlNode n in nodeList)
                {
                    retList.Add(new Event(n));
                }
            }
            return retList;
        }

        public static implicit operator Event(XmlNode node)
        {
            return new Event(node);
        }

        public static explicit operator XmlNode(Event u)
        {
            return u.xmlEventRoot;
        }

        public uint ID
        {
            get { return UInt32.Parse(xmlEventRoot.Attributes[ID_STR].Value); }
        }

        public long Timestamp
        {
            get { return Int64.Parse(xmlTime.InnerText); }
            set { xmlTime.InnerText = value.ToString(); }
        }

        public uint CompartmentID
        {
            get { return UInt32.Parse(xmlCompartmentID.InnerText); }
            set { xmlCompartmentID.InnerText = value.ToString(); }
        }

        public uint UserID
        {
            get { return UInt32.Parse(xmlUserID.InnerText); }
            set { xmlUserID.InnerText = value.ToString(); }
        }

        public uint AssetID
        {
            get { return UInt32.Parse(xmlAssetID.InnerText); }
            set { xmlAssetID.InnerText = value.ToString(); }
        }

        public string DeviceSerial
        {
            get { return xmlDeviceSerial.InnerText; }
            set { xmlDeviceSerial.InnerText = value; }
        }

        public string Content
        {
            get { return xmlContent.InnerText; }
            set { xmlContent.InnerText = value; }
        }
    }
}
